package com.breaktian.demoapp.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.breaktian.share.Config;
import com.breaktian.share.Share;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWxApi();
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initWxApi();
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    private void initWxApi() {
        if (api == null) {
            api = WXAPIFactory.createWXAPI(this, Config.WX_APP_ID, true);
            api.registerApp(Config.WX_APP_ID);
        }
    }

    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            default:
                break;
        }
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        Share share = Share.Registrar.getInstance().getShare();
        if (share != null) {
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    share.success();
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    share.cancel();
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    share.failure("分享失败");
                    break;
            }
        }
        finish();
    }
}